﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Secams.utils
{
    public class DataField
    {
        protected int _fieldIndex;
        protected Type _fieldType;
        protected string _fieldName;
        protected string _propertyName;
        protected string _header;

        protected Dictionary<string, object> _attributes = new Dictionary<string, object>();

        public DataField(int index, string name, Type type, string header)
        {
            fieldIndex = index;
            fieldType = type;
            fieldName = name;
            propertyName = name;
            this.header = header;
        }

        public int fieldIndex
        {
            get { return _fieldIndex; }
            set { _fieldIndex = value; }
        }

        public Type fieldType
        {
            get { return _fieldType; }
            set { _fieldType = value; }
        }

        public string fieldName
        {
            get { return _fieldName; }
            set { _fieldName = value; }
        }

        public string propertyName
        {
            get { return _propertyName; }
            set { _propertyName = value; }
        }

        public string header
        {
            get { return _header; }
            set { _header = value; }
        }


        public Dictionary<string, object> attributes
        {
            get { return _attributes; }
            set { _attributes = value; }
        }

        public void setAttribute(string key, object value)
        {
            attributes[key] = value;
        }

        public object getAttribute(string key)
        {
            return attributes[key];
        }
    }

    public class DataSchema
    {
        protected List<DataField> _fields = new List<DataField>();

        public void addField(string fieldName, Type fieldType, string header)
        {
            addField(new DataField(_fields.Count + 1, fieldName, fieldType, header));
        }


        public void addField(DataField field)
        {
            _fields.Add(field);
        }

        public DataField getField(int index)
        {
            return _fields[index];
        }

        public DataField getField(string fieldName)
        {
            for(int i=0; i < _fields.Count; i++)
            {
                if (_fields[i].fieldName.Equals(fieldName))
                    return getField(i);
            }
            
            throw new KeyNotFoundException("Field "+fieldName+" not found");
        }

        public List<DataField> fields
        {
            get { return _fields; }
        }


        public int size()
        {
            return _fields.Count;
        }

        public string[] stringArrayVals(object obj)
        {
            string[] retval = new string[this.size()];
            int i = 0;
            foreach (DataField field in fields)
            {
                PropertyInfo propInfo = obj.GetType().GetProperty(field.propertyName);
                object valObj = propInfo.GetValue(obj, null);
                retval[i++] = (valObj != null) ? valObj.ToString() : "";
            }
            return retval;

        }
    }
}
